package com.yuandian.management.controller;

import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.service.IAgentIpService;
import com.yuandian.management.vo.AgentConfigManager;
import com.yuandian.management.vo.AgentIpPojo;
import com.yuandian.management.vo.AgentIpVo;
import com.yuandian.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * agentip管理表 前端控制器
 * </p>
 *
 * @author fucx
 * @since 2021-01-27
 */
@RestController
@Api(tags = "agent状态监控-Agent管理")
@RequestMapping("/modules/monitor/agentIp")
public class AgentIpController {
	@Autowired
    private IAgentIpService agentIpService;


    /**
     * 根据条件查询
     *
     * @param searchKey
     * @return
     */
    @GetMapping("/getAgentIpList")
	@ApiOperation(value = "查询数据", notes = "查询数据")
    public R getAgentIpList(@RequestParam(value = "searchKey") String searchKey) {
        if (StringUtils.isEmpty(searchKey)) {
            searchKey = null;
        }

        List<AgentIpVo>  list = agentIpService.queryList(searchKey);
        return R.ok(list);
    }

    /**
     * 更新agentip 别名和主机名和磁盘分区过滤
     *
     * @param agentIps
     * @return
     */
    @GetMapping("/updateAgentByIps")
    @ResponseBody
    public R updateAgentByIps(@RequestParam("agentIps") String agentIps, @RequestParam("aliasNames") String aliasNames,
							  @RequestParam("removeDiskPartitions") String removeDiskPartitions) {
		int num = agentIpService.updateAgentByIps(agentIps, aliasNames, removeDiskPartitions);
		if (num == 0) {
			return R.failed(null, "更新失败！");
		}

		return R.ok();
    }

    /**
     * 更新agentip 别名和主机名和磁盘分区过滤
     *
     * @param agentIp
     * @return
     */
    @PostMapping("/updateAgentByIp")
	@ApiOperation(value = "更新数据", notes = "更新数据")
    public R updateAgentByIp(@RequestBody AgentIpPojo agentIp) {
        agentIp.setUpdateBy(SecurityUtils.getUser().getName());
        agentIp.setUpdateTime(new Date());
        int num = agentIpService.updateByIpSelective(agentIp);
        if (num == 0) {
        	return R.failed(null, "更新失败！");
        }

        Map<String, AgentIpPojo> agentIpMap = AgentConfigManager.agentIpMap;
        AgentIpPojo po = agentIpMap.get(agentIp.getAgentIp());
        if (po != null) {
            po.setAliasName(agentIp.getAliasName());
            po.setRemoveDiskPartition(agentIp.getRemoveDiskPartition());
        }

        return R.ok();
    }

    /**
     * 批量删除
     *
     * @param ips
     * @return
     */
    @DeleteMapping("/deleteAgentByIps/{ips}")
	@ApiOperation(value = "批量删除ip", notes = "批量删除ip")
    public R deleteAgentByIps(@NotNull @PathVariable String[] ips) {
        return agentIpService.deleteAgentByIps(ips);
    }
}

